package com.jojo.bubble.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.FileItem;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.jojo.bubble.entity.User;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradeWapPay {
    public static final String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDspPtT07700mOgksA3e2uo2eOi3X8WM4YBSStdkaPjeESCL1oKlnrV0RUaAonbV5SnqHHcrbm2U9RV0Q3vCIU7rd8C71C9UdQxT2s0wuaBAgKul45QktjmkTKA+Mk47froTqbOBnrBsVQl2xaVNciWA/kfn7yJZYdO/MI8bkbqDH65zDdPNVUBCZsFJDhjSSArXO+yZa5uGZr6ozkDjTs8r3WvcQO36lix/KNIRrcckxCOkQONnOj/+mxRBDUzgPLhU8jpbmfaz1knWk8nYiLPIuy9kG4F/O9OGgB3MpqsQX5gh+68EyVUJ3sCbFej7BrVHS70K7zmjrsIdYU85CTAgMBAAECggEAA7zZve52S2dfuw+vmUKzMDWrBrymiJbsonni5Jlqv0BDlwNwfLhW/PrpEYwyXZQZDTc7WDHFRx/7xE85vGs2fGmaA4dBw9i6QWyXp4QF3EUpBUTsz6s2AImsUh9yHuXnnmRhCW66LZVvnC3wAVBPGsZtDQsaBz1R2VqONz7FBifhAALu8lTkl3b5lNERr8emiIIxtn43ssSOF9yASe1efueKw76wHesPHgd5dDMTG0Pr1FSni0oXoO4cSXiJWfNUoFqvAcZvJbNqLeA4qQMPE90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PUVdEN7wiFO63fAu9QvVHUMU9rNMLmgQICrpeOUJLY5pEygPjJOO366E6mzgZ6wbFUJdsWlTXIlgP5H5+8iWWHTvzCPG5G6gx+ucw3TzVVAQmbBSQ4Y0kgK1zvsmWubhma+qM5A407PK91r3EDt+pYsfyjSEa3HJMQjpEDjZzo//psUQQ1M4Dy4VPI6W5n2s9ZJ1pPJ2IizyLsvZBuBfzvThoAdzKarEF+YIfuvBMlVCd7AmxXo+wa1R0u9Cu85o67CHWFPOQkwIDAQAB";

    public static final String appId = "2021004104654022";

    public static void main(String[] args) throws AlipayApiException {


//        authLogin("95c4d96faf8d49a7a6837e3a3c8fBX36");
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\appCertPublicKey_2021004104654022.crt");
        alipayConfig.setAlipayPublicCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\alipayCertPublicKey_RSA2.crt");
        alipayConfig.setRootCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\alipayRootCert.crt");
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo("70501111111S001111119");
        model.setTotalAmount("9.00");
        model.setSubject("任你游泡泡机");
        model.setProductCode("QUICK_WAP_WAY");
        model.setSellerId("");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    public static User authLogin(String code) throws AlipayApiException {
        User user = new User();
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath("/root/cert/appCertPublicKey_2021004104654022.crt");
        alipayConfig.setAlipayPublicCertPath("/root/cert/alipayCertPublicKey_RSA2.crt");
        alipayConfig.setRootCertPath("/root/cert/alipayRootCert.crt");
//        alipayConfig.setCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\appCertPublicKey_2021004104654022.crt");
//        alipayConfig.setAlipayPublicCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\alipayCertPublicKey_RSA2.crt");
//        alipayConfig.setRootCertPath("D:\\jojo\\bubble\\src\\main\\resources\\cert\\alipayRootCert.crt");
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode(code);
        request.setGrantType("authorization_code");
//        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = alipayClient.certificateExecute(request);
//        System.out.println(response.getBody());
        if (response.isSuccess()) {
            String accessToken = response.getAccessToken(); // 获取访问令牌
            String userId = response.getUserId(); // 获取支付宝用户ID
            AlipayUserInfoShareRequest infoRequest = new AlipayUserInfoShareRequest();
            AlipayUserInfoShareResponse infoResponse = alipayClient.certificateExecute(infoRequest, accessToken);

            String nickName = infoResponse.getNickName(); // 用户昵称
            String avatar = infoResponse.getAvatar(); // 用户头像URL
//            System.out.println(nickName + avatar);
            user.setNickname(nickName);
            user.setAvatar(avatar);
            user.setOpenId(userId);
            user.setType(0);
            user.setAccessToken(accessToken);
            System.out.println("调用用户信息成功");
        } else {
            System.out.println("调用用户信息失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return user;
    }

    public static String aliAppletPay(String orderNo, String price, String devCode, String runTime) throws AlipayApiException {
        CertAlipayRequest alipayConfig = new CertAlipayRequest();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCertPath("/root/cert/appCertPublicKey_2021004104654022.crt");
        alipayConfig.setAlipayPublicCertPath("/root/cert/alipayCertPublicKey_RSA2.crt");
        alipayConfig.setRootCertPath("/root/cert/alipayRootCert.crt");
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl("https://ppj.renniyou.com.cn/h5/#/pages/index/run?min=" + runTime + "&devCode=" + devCode + "&orderNo=" + orderNo);
        request.setNotifyUrl("https://bubble.renniyou.com.cn/api/order/ali/notifyUrl");
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(orderNo);
        model.setTotalAmount(price);
//        model.setEnablePayChannels("pcredit,moneyFund,debitCardExpress");
        model.setSubject("任你游泡泡机");
        model.setProductCode("QUICK_WAP_WAY");
        model.setSellerId("");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return response.getBody();
        } else {
            System.out.println("调用失败");

            return null;
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}